﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliQuartz
 * 唯一标识：3c9089b3-7063-4f0d-b904-3479d605d71d
 * 文件名：MesCuringRequest
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/6/8 14:44:20
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using K8.CBB;
//using KingerRobot.MexicoSailunMDC.Dao;

namespace KingerRobot.MexicoSailunQuartz;

public class MesCuringRequest
{

    private static ConcurrentDictionary<string, DateTime> directory = new();
    private int second = 60;
   [ Obsolete]
    public void RequestMaterial()
    {

        //Logging.XLog.Log(new Logging.Log { Data = $"开始同步硫化请料" });
        try
        {
            // 获取MES接口硫化机台状态（中间表）数据
            var mesService = K8.RSF.Client.Create<IMiddleMesService>();
            //Logging.XLog.Log(new Logging.Log { Data = $"硫化请料1043" });
            //获取到所有可以请料的机台
            var requests = mesService.GetRequestMaterialInfos();
            var curList = new List<TcmInfo>();
            //将马上满料但是输送完未满30秒的机台筛出
            foreach (var item in requests)
            {
                //if (item.MaxCacheCount - item.RealCacheCount <= 1)
                //{
                //    //即将满了
                //    if (item.UpdateTime > DateTime.Now.AddSeconds(-30))
                //    {
                //        //没到时间
                //        continue;
                //    }
                //}
                curList.Add(item);
            }
            requests = curList;
            if (requests is null || requests.Count == 0)
            {
                K8.LSF.Client.Log(new K8.LSF.Log { Data = $"未获取到满足条件的硫化机请料信息" });
                return;
            }



            // 按照硫化机请料
            var curingCodes = requests.OrderBy(t => t.REQ_REG_DT).Select(t => t.TCM_NO).Distinct().ToList();
            foreach (var curingCode in curingCodes)
            {
                var requestMaterials = requests.Where(t => t.TCM_NO == curingCode).ToList();

                // 单胎请料等待时长
                //if (false)
                //{
                if (requestMaterials.Count == 1)
                {
                    // 判断是否初始数据
                    var flag = directory.TryGetValue(curingCode, out DateTime dateTime);
                    if (flag)
                    {
                        var nowDate = DateTime.Now;
                        if (nowDate <= dateTime.AddSeconds(second))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        directory.TryAdd(curingCode, DateTime.Now);
                        continue;
                    }
                }
                directory.TryRemove(curingCode, out DateTime date);
                //}
                //Logging.XLog.Log(new Logging.Log { Data = $"硫化请料1086" });

                var materials = requestMaterials.Select(t => new RequestMaterialBean
                {
                    //Id = t.Id.GetValueOrDefault().ToString(),
                    CuringCode = t.TCM_NO,
                    CuringMode = t.TCM_DIR == "R" ? 1 : 2,

                    Serno = t.TBM_PS,
                    Spec = t.TCM_SPEC,
                    //InvQty = t.PlanCuringCount,

                    //CanQty = t.CanQty,
                    Remark = t.TCM_DESC,
                }).ToList();
                //Logging.XLog.Log(new Logging.Log { Data = $"硫化请料1098" });//2023-07-29 20:59:05.235     //20:59:02.254
                var wcsService = K8.RSF.Client.Create<ICuringService>();
                var results = wcsService.RequestMaterialByMiddleTable(materials);

            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.ToString() });
        }
    }

    /// <summary>
    /// 创建出库订单
    /// </summary>
    public void CreateOutSummaries()
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始创建出库请料单" });
            var outService = K8.RSF.Client.Create<IWarehousingOutService>();
            outService.CreateOutSummaries();
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"创建出库请料单完成" });
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"创建出库请料单异常,{ex.ToString()}" });
        }
    }

    //public void RequestMaterial()
    //{

    //    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始同步硫化请料" });
    //    try
    //    {
    //        // 获取MES接口硫化机台状态（中间表）数据
    //        var mesService = K8.RSF.Client.Create<IMiddleMesService>();
    //        //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1043" });
    //        //获取到所有可以请料的机台
    //        var requests = mesService.GetRequestMaterialInfos();
    //        var curList = new List<RequestMaterialInfo>();
    //        //将马上满料但是输送完未满30秒的机台筛出
    //        foreach (var item in requests)
    //        {
    //            if (item.MaxCacheCount - item.RealCacheCount <= 1)
    //            {
    //                //即将满了
    //                if (item.UpdateTime > DateTime.Now.AddSeconds(-30))
    //                {
    //                    //没到时间
    //                    continue;
    //                }
    //            }
    //            curList.Add(item);
    //        }
    //        requests = curList;
    //        //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1045" });
    //        if (requests is null || requests.Count == 0)
    //        {
    //            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"未获取到满足条件的硫化机请料信息" });
    //            return;
    //        }



    //        //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1054" });
    //        // 按照硫化机请料
    //        var curingCodes = requests.OrderBy(t => t.UpdateTime).Select(t => t.EquipNo).Distinct().ToList();
    //        foreach (var curingCode in curingCodes)
    //        {
    //            //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1058" });//2023-07-29 20:59:05.177
    //            var requestMaterials = requests.Where(t => t.EquipNo == curingCode).ToList();
    //            //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1059" });

    //            // 单胎请料等待时长
    //            //if (false)
    //            //{
    //            if (requestMaterials.Count == 1)
    //            {
    //                // 判断是否初始数据
    //                var flag = directory.TryGetValue(curingCode, out DateTime dateTime);
    //                if (flag)
    //                {
    //                    var nowDate = DateTime.Now;
    //                    if (nowDate <= dateTime.AddSeconds(second))
    //                    {
    //                        continue;
    //                    }
    //                }
    //                else
    //                {
    //                    directory.TryAdd(curingCode, DateTime.Now);
    //                    continue;
    //                }
    //            }
    //            directory.TryRemove(curingCode, out DateTime date);
    //            //}
    //            //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1086" });

    //            var materials = requestMaterials.Select(t => new RequestMaterialBean
    //            {
    //                Id = t.Id.GetValueOrDefault().ToString(),
    //                CuringCode = t.EquipNo,
    //                CuringMode = t.EquipModel,

    //                Serno = t.Id.GetValueOrDefault().ToString(),
    //                Spec = t.MaterialCode,
    //                InvQty = t.PlanCuringCount,

    //                CanQty = t.CanQty,
    //                Remark = t.Remark,
    //            }).ToList();
    //            //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1098" });//2023-07-29 20:59:05.235     //20:59:02.254
    //            var wcsService = K8.RSF.Client.Create<ICuringService>();
    //            var results = wcsService.RequestMaterialByMiddleTable(materials);
    //            //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"硫化请料1101" });//2023-07-29 20:59:05.337     //2023-07-29 20:59:03.239
    //            //foreach (var result in results)
    //            //{
    //            //    // 请料订单未创建成功,不更新请料中间表
    //            //    if (result.SummaryId == 0) continue;
    //            //    var num = mesService.UpdateRequestMaterialInfos(new UpdateRequestMaterial
    //            //    {
    //            //        Id = result.Id,
    //            //        equipNo = result.EquipNo,
    //            //        equipModel = result.EquipModel,
    //            //        SummaryId = result.SummaryId
    //            //    });
    //            //}
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.ToString() });
    //    }
    //}

    ///// <summary>
    ///// 7.同步 硫化机台状态（中间表）
    ///// </summary>
    //public void MesSyncRequestMaterial()
    //{
    //    // 获取生产数据
    //    var mesService = K8.RSF.Client.Create<IMiddleMesService>();
    //    var requestMaterialInfos = mesService.GetRequestMaterials();

    //    var materials = requestMaterialInfos.Select(t => new SyncRequestMaterialBean
    //    {
    //        EquipNo = t.EquipNo,
    //        EquipModel = t.EquipModel,
    //        MaterialId = t.MaterialId,
    //        MaterialCode = t.MaterialCode,
    //        MaxCacheCount = t.MaxCacheCount,
    //        RealCacheCount = t.RealCacheCount,
    //        CuringTime = t.CuringTime,
    //        CurrentCuringTime = t.CurrentCuringTime,
    //        EquipStatusMes = t.EquipStatusMes,
    //        EquipStatusHost = t.EquipStatusHost,
    //        CuringTotalTime = t.CuringTotalTime,
    //        GreentyrenoMes = t.GreentyrenoMes,
    //        IsEnableRequest = t.IsEnableRequest,
    //        HostWmsFlag=t.HostWmsFlag,
    //    }).ToList();

    //    // 同步生产数据
    //    var wcsService = K8.RSF.Client.Create<ICuringService>();

    //    wcsService.MesSyncRequestMaterial(materials);
    //}


    //public void RequestManualCuring()
    //{

    //    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始同步手动放胎" });
    //    //获取上一次读取的时间 继续往表里插

    //    var curService = K8.RSF.Client.Create<ICuringService>();
    //    var lastManualCuring = curService.GetFirstManualCuringRecordDesc();
    //    DateTime lastPutTime = Convert.ToDateTime("2024-02-22 15:00:00");
    //    //DateTime endPutTime = DateTime.Now;
    //    var productCode = "";
    //    if (lastManualCuring is not null)
    //    {
    //        lastPutTime = lastManualCuring.MesPutTime ?? Convert.ToDateTime("2024-02-22 15:00");
    //        productCode = lastManualCuring.ProductCode;
    //    }
    //    var mesService = K8.RSF.Client.Create<IMiddleMesService>();
    //    //var mesManualList = mesService.GetManualScanRecByTime(lastPutTime, endPutTime, productCode);
    //    var mesManualList = mesService.GetManualScanRec();
    //    mesManualList = mesManualList.Where(t => t.ConsumeTimeT > lastPutTime).ToList();
    //    foreach (var mesManual in mesManualList)
    //    {
    //        try
    //        {
    //            curService.AddManualCuringRecordByProductCode(mesManual.WorkCenterNoS, mesManual.TyreNoS, mesManual.ConsumeTimeT);
    //        }
    //        catch (Exception ex)
    //        {
    //            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"同步手动放胎异常,{ex.ToString()}" });
    //        }
    //    }
    //    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"同步手动放胎完成" });


    //}




}

